﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoneyWentWhere.OutputHelpers;
using MoneyWentWhere.Utils;

namespace MoneyWentWhere.OutputHelpers
{
    public abstract class DisplayConverter
    {
        Alignment _alignment = Alignment.Left;
        public Alignment Alignment 
        { 
            get { return _alignment; }
            protected set { _alignment = value; }
        }

        public abstract String FieldToString(object value);
    }

    /// <summary>
    /// Default base class for converters
    /// </summary>
    class DefaultDisplayConverter : DisplayConverter
    {
        String _format = "{0}";

        /// <summary>
        /// Converts the item to string.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override String FieldToString(object value)
        {
            if (value == null) { return ""; }
            ChooseFormatAndAlignment(value);

            return String.Format(_format, value);
        }

        void ChooseFormatAndAlignment(object value)
        {
            _format = "{0}";
            Alignment = Alignment.Left;

            if (value == null) { return; }

            Type type = value.GetType();
            if (type == typeof(decimal))
            {
                Alignment = Alignment.Right;
                _format = "{0:#,##0.00}";
            }
            else if (type == typeof(int) ||
                     type == typeof(double) ||
                     type == typeof(float))
            {
                Alignment = Alignment.Right;
            }
            else if (type == typeof(DateTime))
            {
                DateTime dt = (DateTime)value;
                if (dt == dt.Date)
                {
                    _format = "{0:yyyy-MM-dd}";
                }
                else if (dt.Second == 0)
                {
                    _format = "{0:yyyy-MM-dd HH:mm}";
                }
                else
                {
                    _format = "{0:yyyy-MM-dd HH:mm:ss}";
                }

            }
        }
    }
}
